package com.example.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commonutils.ordervo.CourseForOrder;
import com.example.commonutils.ordervo.MemberForOrder;
import com.example.order.entity.Order;
import com.example.order.mapper.OrderMapper;
import com.example.order.service.OrderService;
import com.example.order.utils.OrderNoUtil;
import com.example.servicebase.client.EduClient;
import com.example.servicebase.client.UcenterClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private EduClient eduClient;

    @Qualifier("com.example.servicebase.client.UcenterClient")
    @Autowired
    private UcenterClient centerClient;

    @Override
    public String createOrder(String courseId, String memberId) {
        //根据课程id获取课程信息
        CourseForOrder courseDto = eduClient.getCourseForOrder(courseId);
        //根据用户id获取用户信息
        MemberForOrder ucenterMember = centerClient.getUserForOrder(memberId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);//订单状态 0表示未支付 1表示已支付
        order.setPayType(1); //支付方式，1表示微信
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
